<?php
/**
*	cli模式运行可用 php api.php cli,run
*/
class cliClassAction extends apiAction
{
	public $logincheck = false;
	
	
	/**
	*	
	*/
	public function runAction()
	{
		return 'cli mode';
	}
	
	public function testAction()
	{
		$rand = $this->getparams('rand');
		c('cache')->set('pushtest', $rand, 10);
	}
	
	/**
	*	ip读取归属地
	*/
	public function ipstrAction()
	{
		$id = (int)$this->getparams('id','0');
		if($id==0)return;
		$prs= m('party')->getone($id);
		if(!$prs)return;
		$ip = $prs['ip'];
		if(isempt($ip) || !isempt($prs['city']))return;
		
		$url = 'http://ip-api.com/json/'.$ip.'?lang=zh-CN';
		$result = c('curl')->getcurl($url);
		if($result){
			$barr = json_decode($result, true);
			if(arrvalue($barr,'status')=='success'){
				$sheng= $barr['regionName'];
				$city = $barr['city'];
				m('party')->update(array(
					'sheng' => $sheng,
					'city' => $city,
				),$id);
			}
		}
		return $result;
	}
	
	/**
	*	异步推送
	*/
	public function xmpushAction()
	{
		$wfuid = $this->getparams('wfuid');
		if(!$wfuid)return;
		$title = $this->getparams('title');
		$mess  = $this->getparams('mess');
		$rows  = m('login')->getall('`uid` in('.$wfuid.') and `ispush`=1');
		$hwarr = $xmarr = $iosarr = array();
		foreach($rows as $k=>$rs){
			$token = $rs['pushtoken'];
			if($rs['cfrom']=='appios'){
				$iosarr[] = $token;
			}else{
				if(contain($rs['web'],'huawei')){
					$hwarr[]= $token;
				}else{
					$xmarr[]= $token;
				}
			}
		}
		$msg = '';
		$cobj= c('xmpush');
		$lx	 = '';
		if($xmarr){
			if($cobj->sendandroidbool()){
				$msg = $cobj->androidsend($xmarr, $this->jm->base64decode($title), $this->jm->base64decode($mess));
			}else{
				$msg = c('xinhu')->getdata('send',array(
					'm' => 'apppush',
					'title' => $title,
					'mess'  => $mess,
					'xmarr' => join(',', $xmarr)
				), 1);
			}
			$lx = 'xmandroid';
		}
		if($iosarr){
			if($cobj->sendiossend()){
				$msg = $cobj->iossend($iosarr, $this->jm->base64decode($title), $this->jm->base64decode($mess));
			}else{
				$msg = c('xinhu')->getdata('send',array(
					'm' => 'apppush',
					'title' => $title,
					'mess'  => $mess,
					'iosarr' => join(',', $iosarr)
				), 1);
			}
			$lx = 'xmios';
		}
		if($hwarr){
			if($cobj->sendhuaweisend()){
				$msg = $cobj->hwandroidsend($hwarr, $this->jm->base64decode($title), $this->jm->base64decode($mess));
			}else{
				$msg = c('xinhu')->getdata('send',array(
					'm' => 'apppush',
					'title' => $title,
					'mess'  => $mess,
					'hwarr' => join(',', $hwarr)
				), 1);
			}
			$lx = 'huawei';
		}
		if($msg){
			$this->rock->debugs(array(
				'barrmsg' => $msg,
				'title'   => $title,
				'mess'    => $mess,
			), 'push_'.$lx.'');
		}
		
		return $msg;
	}
	
	public function hdiaosendAction()
	{
		$urls = $this->getparams('urla');
		$body = $this->getparams('body');
		$urla = explode(',', $this->jm->base64decode($urls));
		$msg  = '';
		foreach($urla as $url){
			$msg .= c('curl')->postcurl($url, $body);
		}
		return $msg;
	}
}